#include "ActorDonkeyKong.h"

ActorDonkeyKong::ActorDonkeyKong(CL_GraphicContext gc)
{
	grafic = new SpriteDonkeyKong(gc);
	setPosition(110, 180);
	animationdelay = 0;
	barreltimer = 0;
	numberofbarrels = 0;
	collision = CollisionDetection::giveme();
	playerlost = false;
	throwtimer=0;
	waitingbarrel=-1;
	score=0;
	LastBonusTime=0;
	LastBonusBarrel=-1;
	msound=new Sound();
}

ActorDonkeyKong::~ActorDonkeyKong(){
	delete grafic;
	delete msound;
}

void ActorDonkeyKong::throwbarrel()
{
	StateSet* set = StateSetFactory::giveme()->givemestateset(numberofbarrels%4);
	barrels[numberofbarrels] = new ActorBarrel(grafic->donkeyKongContext, set);
	numberofbarrels++;
}

bool ActorDonkeyKong::fixedstanding()
{
	int i = 0;
	animationdelay = animationdelay%40;
	if(waitingbarrel==-1){
		for(int i=0;i<numberofbarrels;i++){
			if(barrels[i]->ready==true){
				barrels[i]->ready=false;
				waitingbarrel=i;
				grafic->donkeyKongSprites[1]->rotate_yaw(CL_Angle(180, cl_degrees));
				grafic->drawthrowingdonkey(getPosition());
				throwtimer=1;
				break;
			}
		}
	}
	i=0;
	if(barreltimer%300 == 0 && numberofbarrels < MAX_BARRELS && throwtimer==0)
	{
		grafic->donkeyKongSprites[1]->rotate_yaw(CL_Angle(180, cl_degrees));
		grafic->drawthrowingdonkey(getPosition());
		throwtimer=1;
	}else if(throwtimer>60){
		grafic->drawfixedstandingdonkey(getPosition());
		throwtimer=0;
	}else if(throwtimer>30){
		grafic->drawthrowingdonkey(getPosition());
		throwtimer++;
	}else if(throwtimer==30){
		grafic->donkeyKongSprites[1]->rotate_yaw(CL_Angle(180, cl_degrees));
		grafic->drawthrowingdonkey(getPosition());
		if(waitingbarrel!=-1){
			barrels[waitingbarrel]->reset();
			waitingbarrel=-1;
		}else{
			throwbarrel();
		}
		throwtimer++;
	}else if(throwtimer>0){
		grafic->drawthrowingdonkey(getPosition());
		throwtimer++;
	}
	else{
		grafic->drawfixedstandingdonkey(getPosition());
	}
	if(animationdelay == 0)
	{
		grafic->donkeyKongSprites[0]->rotate_yaw(CL_Angle(180, cl_degrees));
	}
	barreltimer++;
	animationdelay++;


	while(i < numberofbarrels)
	{
		barrels[i]->process();
		if(collision->mariobarrel(barrels[i]->getPosition()))
		{
			playerlost = true;
//			barrels[i]->drawabarrel(CL_Vec2<int>(10, 10));
		}
		if(collision->mariooverbarrel(barrels[i]->getPosition())&&!playerlost){
			if((CL_System::get_time()>LastBonusTime+1000)||LastBonusBarrel!=i){
				score+=100;
				LastBonusTime=CL_System::get_time();
				LastBonusBarrel=i;
			}
		}
		i++;
	}

	grafic->drawpoints(score);
	return true;
}

void ActorDonkeyKong::loosingdonkey()
{
	setPosition(getPosition().x, getPosition().y+1);
	grafic->donkeyKongSprites[0]->rotate(CL_Angle(1, cl_degrees));
	grafic->drawfixedstandingdonkey(getPosition());
}

void ActorDonkeyKong::reset(bool gameover)
{
	setPosition(110, 180);
	animationdelay = 0;
	barreltimer = 0;
	numberofbarrels = 0;
	collision = CollisionDetection::giveme();
	playerlost = false;
	if(gameover == true)
	{
		score=0;
	}
	grafic->donkeyKongSprites[0]->rotate(grafic->donkeyKongSprites[0]->get_angle()*-1);
}

bool ActorDonkeyKong::hasplayerlost()
{
	return playerlost;
}